package com.youme.section.shop.adapter

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.hyphenate.easeim.R
import com.youme.model.Rest
import com.youme.section.shop.ShopDetailsActivity

class HotDianAdapter(private val textList: ArrayList<Rest>, val context: Activity) :
    RecyclerView.Adapter<HotDianAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.adapter_favorite_item, parent, false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int = textList.size ?: 0

    override fun onBindViewHolder(holder: HotDianAdapter.MyViewHolder, position: Int) {
        val textpos = textList[position]
        holder.name.text = textpos.restName
        holder.juli.text = textpos.distance
        holder.prices.text = "¥" + textpos.userPrice + "/人"
        holder.imageIcon
        Glide.with(context)
            .load(textpos.restPic).into(holder.imageIcon)
        //图片加载失败后，显示的图片
        holder.layout.setOnClickListener {
            val intent = Intent()
            intent.putExtra("restId", textpos.restId)
            intent.setClass(context, ShopDetailsActivity().javaClass)
            context.startActivity(intent)
        }
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val name: TextView = itemView.findViewById(R.id.tv_dian_name)
        val juli: TextView = itemView.findViewById(R.id.tv_juli)
        val prices: TextView = itemView.findViewById(R.id.tv_prices)
        val imageIcon: ImageView = itemView.findViewById(R.id.im_favorite)
        val layout: LinearLayout = itemView.findViewById(R.id.view_content);
    }
}